package com.lottery.game.controller.v2.game.blank.acount;

import java.math.BigDecimal;

import com.lottery.game.annotation.Current;
import com.lottery.game.common.constants.GlobalLotteryConstants;
import com.lottery.game.core.model.dto.UserToken;
import com.lottery.game.core.modules.entity.user.UpAndReport;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lottery.game.common.Response;
import com.lottery.game.common.exception.BaseException;
import com.lottery.game.constants.RespErrorCodes;
import com.lottery.game.core.modules.entity.blank.acount.ReceivingBankAccountSec;
import com.lottery.game.core.modules.entity.blank.acount.ReceivingBankAccountVo;
import com.lottery.game.core.modules.entity.user.UpAndDownReport;
import com.lottery.game.core.modules.entity.user.UpAndDownReportVo;
import com.lottery.game.core.modules.service.app.UpAndDownReportService;
import com.lottery.game.core.modules.service.blank.account.BlankAcountService;
import com.lottery.game.model.FeedbackRequest;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/api/v2/blank/acount")
@Api(tags = "APP-收款账户")
@Slf4j
public class BlankAcountController {

	private static final String SIGN_KEY = "callback-manager-sign";
	@Autowired
	private BlankAcountService blankAcountService;
	
	 @Autowired
	 private UpAndDownReportService upAndDownReportService;
	
	@ApiOperation(value = "充值账户列表", notes = "充值账户列表")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "令牌ticket", dataType = "String", paramType = "header",required = true),
    })
	public Response<PageInfo<ReceivingBankAccountSec>> list(@ApiIgnore @Current UserToken userToken, @RequestBody ReceivingBankAccountVo receivingBankAccountVo){
		String jsonString = JSON.toJSONString(receivingBankAccountVo);
		ReceivingBankAccountSec receivingBankAccount = JSON.parseObject(jsonString, ReceivingBankAccountSec.class);
		PageInfo<ReceivingBankAccountSec> pageInfo = blankAcountService.findReceivingBankAccount(receivingBankAccount);
		return Response.build(pageInfo);
	}
	

	 /**
     * 上下分是否通过反馈 - 推送Ws消息给前端
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "充值提现成功或失败反馈", notes = "充值提现成功或失败反馈", httpMethod="POST")
    @PostMapping(value = "/callback/of/up/or/down", produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
    private Response callbackOfUpOrDown(
            @ApiParam(name = "上下分反馈请求", value = "传入json格式", required = true)
            @RequestBody FeedbackRequest request) {
        if (!request.getSign().equals(SIGN_KEY)) {
            throw new BaseException("非法操作！");
        }
        try {
            String userId = request.getUserId();
            Boolean result = Boolean.valueOf(request.getResult());
            String balanceStr = request.getBalance();
            BigDecimal balance = null;
            if (!StringUtils.isEmpty(balanceStr)) {
                balance = BigDecimal.valueOf(Double.valueOf(request.getBalance()));
            }
            upAndDownReportService.upAndDownFeedbaakPublish(userId,result,balance);
            return Response.buildSuccess();
        } catch (Exception e){
            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("操作失败!稍后再试");
        }
    }

    @SuppressWarnings("unchecked")
    @ApiOperation(value = "充值金额提交", notes = "充值金额提交")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "gameuserId", value = "用户ID(申请提交的用户ID)"),
            @ApiImplicitParam(name = "nickname", value = "昵称", dataType = "String"),
            @ApiImplicitParam(name = "photourl", value = "头像", dataType = "String"),
            @ApiImplicitParam(name = "bankAccount", value = "账号", dataType = "String"),
            @ApiImplicitParam(name = "payType", value = "支付类型(支付宝还是微信)", dataType = "String",required = true,example = "支付宝"),
            @ApiImplicitParam(name = "applyMoneyAmount", value = "申请金额", dataType = "String",required = true,example = "10")
    })*/
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "令牌ticket", dataType = "String", paramType = "header",required = true),
    })
    @RequestMapping(value = "/saveUp", method = RequestMethod.POST,produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Response<String> saveUp(@ApiIgnore @Current UserToken userToken, @RequestBody UpAndReport report){
        try {
            if(!userToken.getPhone().equals(report.getBankAccount())){
                return Response.buildSuccess("银行账户与登录账号不一致！"+userToken.getPhone());
            }
            UpAndDownReport upAndDownReport = new UpAndDownReport();
            //复制
            BeanUtils.copyProperties(report,upAndDownReport);
            int i = blankAcountService.saveUp(upAndDownReport, upAndDownReport.getGameuserId());
            if (GlobalLotteryConstants.RES_SUCCESS == i){
                return Response.buildSuccess("充值成功");

            }else  {
                return Response.buildError("充值正在处理中 请勿重复提交！");
            }
        }catch (Exception e){
            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("操作失败!稍后再试");
        }

    }
}
